var util = require('util');
var config = require('./helper.js').config;

function print_object(root, obj, names, argv) {
    for (var i = 0; i < names.length; ++i) {
        var name = names[i];
        if ('object' != typeof(obj)) {
            return;
        }

        if ('*' == name) {
            var subNames = [];
            for (var si = i+1; si < names.length; ++si) {
                subNames.push(names[si]);
            }

            for (var key in obj) {
                print_object(root, obj[key], subNames, argv);
            }

            return;
        } else if ('keys' == name) {
            var filter = names[i+1];
            for (var key in obj) {
                if ('num' == filter && isNaN(key)) {
                    continue;
                }
                print_object(root, key, [], argv);
            }
            return;
        }

        obj = obj[name];
    }

    if ('function' == typeof(obj)) {
        var value = obj.apply(root, argv);
        if (value instanceof Array) {
            console.log(value.join(' '));
        } else if (null != value) {
            console.log(value);
        }
    } else if (null != obj) {
        if (obj instanceof Array) {
            console.log(obj.join(' '));
        } else {
            console.log(obj);
        }
    }
}

function print_config() {
    if (process.argv.length < 3) {
        process.exit(1);
    }
    
    var argv = [];
    for (var i = 3; i < process.argv.length; ++i) {
        argv.push(process.argv[i]);
    }

    var names = process.argv[2].split(".");
    print_object(config, config, names, argv);

    process.exit(0);
}

print_config();
//process.on('uncaughtException', function (err) {
//    process.exit(1);
//});
